/*
 * One example for NOI CSP-J Lesson 6:
 * <https://courses.fmsoft.cn/plzs/noijunior-search-algorithms.html>
 *
 * Author: Vincent Wei
 *  - <https://github.com/VincentWei>
 *  - <https://gitee.com/vincentwei7>
 *
 * Copyright (C) 2025 FMSoft <https://www.fmsoft.cn>.
 * License: GPLv3
 */

#include <iostream>     // std::cout
#include <algorithm>    // std::search
#include <vector>       // std::vector

int main ()
{
    std::vector<int> haystack;

    // set some values:        haystack: 10 20 30 40 50 60 70 80 90
    for (int i=1; i<10; i++)
        haystack.push_back(i*10);

    // using default comparison:
    int needle1[] = {40,50,60,70};
    std::vector<int>::iterator it;
    it = std::search(haystack.begin(), haystack.end(), needle1, needle1+4);

    if (it != haystack.end())
        std::cout << "needle1 found at position " << (it - haystack.begin()) <<
            std::endl;
    else
        std::cout << "needle1 not found\n";

    int needle2 = 40;
    it = std::search_n (haystack.begin(), haystack.end(), 1, needle2);
    if (it != haystack.end())
        std::cout << "needle2 (one element) found at position " <<
            (it - haystack.begin()) << std::endl;
    else
        std::cout << "needle2 (one) not found\n";

    int needle3 = 40;
    it = std::search_n (haystack.begin(), haystack.end(), 2, needle3);
    if (it != haystack.end())
        std::cout << "needle3 (two same elements) found at position " <<
            (it - haystack.begin()) << std::endl;
    else
        std::cout << "needle3 (two same elements) not found\n";

    return 0;
}

